package org.kjtc.mapper;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.jdbc.SQL;
import org.kjtc.entity.ChargingStation;
import org.kjtc.entity.Equipment;

/**
 * @ Author     ：Yellowong
 * @ Date       ：Created in 11:29 2018/12/10
 * @ Description：
 */
public class EquipmentProvider {

    public String getEquipmentList(Equipment equipment) {

        String sqlStr;

        /* 确认查询字段 */
        sqlStr = "e.equipment_id AS equipmentID, " +
                "e.equipment_name AS equipmentName, " +
                "e.charging_station_id AS chargingStationID, " +
                "c.charging_station_name AS chargingStationName, " +
                "e.equipment_power AS equipmentPower, " +
                "e.equipment_type AS equipmentType, " +
                "e.business_id AS businessID, " +
                "e.manufacturer AS manufacturer, " +
                "e.manufacturer_dttm AS manufacturerDTTM " +
                "FROM equipment e " +
                "LEFT JOIN charging_station c " +   // 左连接充电站表
                "ON e.charging_station_id = c.charging_station_id " +   // 连接条件
                "WHERE 1 = 1 ";     // 永远为true的查询条件，便于添加新的查询条件

        /* 添加有效的查询条件WHERE */
        if (!StringUtils.isEmpty(equipment.getChargingStationID()))        // 根据输入的充电站名称(转化为ID)
            sqlStr += "AND e.charging_station_id = #{chargingStationID} ";
        if (equipment.getEquipmentType() >= 0)       // 根据设备类型
            sqlStr += "AND e.equipment_type = #{equipmentType} ";

        /* 排序 */
        SQL sql = new SQL().SELECT(sqlStr);
        sql.ORDER_BY("e.equipment_id ");

        return sql.toString();
    }

    /* 统计不同设备类型的个数 */
    public String equipmentStatusStatistics() {

        return "SELECT equipment_type AS type, COUNT(*) AS num FROM equipment GROUP BY equipment_type";
    }

    /* 获取所有充电站ID及名称 */
    public String getChargingStationName(ChargingStation chargingStation) {

        String sqlStr;

        sqlStr = "charging_station_id AS chargingStationID, " +
                "charging_station_name AS chargingStationName " +
                "FROM charging_station " +
                "WHERE 1=1";

        if (!StringUtils.isEmpty(chargingStation.getChargingStationID())) {
            sqlStr += " AND charging_station_id = #{chargingStationID}";
        }

        SQL sql = new SQL().SELECT(sqlStr);

        return sql.toString();
    }
}
